package com.xhj.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhj.entity.ExcelSubjectData;
import com.xhj.entity.Subject;
import com.xhj.entity.SubjectData;
import com.xhj.listener.SubjectExcelListener;
import com.xhj.mapper.SubjectMapper;
import com.xhj.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public void importSubjectData(MultipartFile file,SubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("添加课程分类失败");
        }

    }

    @Override
    public List<SubjectData> getSubjectListData() {
        // 获得数据库中所有subject对象
        List<Subject> subjectList = subjectMapper.selectList(null);
        // 判断subjectList是否有效
        if (subjectList != null && subjectList.size() > 0){
            List<SubjectData> subjectDataList = new ArrayList<>();
            // 根据ID存放SubjectData对象
            Map<String, SubjectData> subjectDataMap = new HashMap<>();
            // 遍历subjectList
            for (Subject subject : subjectList) {
                // 该课程parent_id为0 则表示是一级课程 放入subjectDataList中
                if (subject.getParentId().equals("0")){
                    SubjectData subjectData = new SubjectData();
                    copyProperties(subject,subjectData);
                    subjectDataList.add(subjectData);
                    subjectDataMap.put(subjectData.getId(),subjectData);
                }else {
                    // 获得parent_id
                    String parentId = subject.getParentId();
                    SubjectData subjectData = new SubjectData();
                    copyProperties(subject,subjectData);
                    // 获得父节点subjectData
                    SubjectData parentSubjectData = subjectDataMap.get(parentId);
                    // 将subjectData存入parentSubjectData的children集合中
                    parentSubjectData.getChildren().add(subjectData);
                    subjectDataMap.put(subjectData.getId(),subjectData);
                }
            }
            return subjectDataList;
        }
        return null;
    }

    private void copyProperties(Subject subject,SubjectData subjectData){
        subjectData.setId(subject.getId());
        subjectData.setLabel(subject.getTitle());
        List<SubjectData> children = new ArrayList<>();
        subjectData.setChildren(children);
    }
}
